package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.minio.MinIOFileOptions;
import com.heima.common.util.RequestContextUtil;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService {
    /**
     * TODO:素材上传
     * @param multipartFile
     * @return
     */
    @Autowired
    MinIOFileOptions minIOFileOptions;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws Exception {
        // 1 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 2 防止重名,UUID覆盖重写+文件后缀名
        //   取文件后缀名->StringUtils.getFilenameExtension(fileName)
        String name=UUID.randomUUID().toString().replace("-","")+
                "."+
                StringUtils.getFilenameExtension(fileName);
        // 3 上传MinIO并返回url
        String url = minIOFileOptions.uploadImgFile("/image", name, multipartFile.getInputStream());
        // 4 new一个对象,整合url写入数据库
        WmMaterial material = new WmMaterial();
        Integer userId = RequestContextUtil.get("id");
//        material.setUserId(1);//暂时硬编码,后续解析token,获取其中的userID
        material.setUserId(userId);//暂时硬编码,后续解析token,获取其中的userID
        material.setCreatedTime(new Date());
        material.setIsCollection(0);
        material.setType(0);
        material.setUrl(url);
        save(material);
        // 5 返回结果
        return ResponseResult.okResult(material);
    }

    /**
     * TODO:素材列表查询
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult listPicture(WmMaterialDto wmMaterialDto) {
        Page<WmMaterial> page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //判断是否收藏，是-》查询收藏-》收藏的排在前面
        if (wmMaterialDto.getIsCollection()!=null && wmMaterialDto.getIsCollection()==1) {
            wrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        // 解析token获取userId
        Integer userId = RequestContextUtil.get("id");
        wrapper.eq(WmMaterial::getUserId,userId)
                .orderByDesc(WmMaterial::getCreatedTime);
        // 分页查询结果
        Page<WmMaterial> pageInfo = page(page, wrapper);
        // 封装结果类：当前页、当前页大小、当前页记录数、当前页记录
        PageResponseResult result = new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(),(int)pageInfo.getTotal());
        result.setData(pageInfo.getRecords());
        return result;
    }
}
